package com.adee.java.json;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;

public class Test002_Gson {
    public static void main(String[] args) {
        test2();
    }

    // 基本使用
    public static void test1() {
        Gson gson = new Gson();
        User user = new User(true);
        System.out.println(user.toString());
        String s = gson.toJson(user);
        System.out.println(s);
        User user1 = gson.fromJson(s, User.class);
        System.out.println(user1.toString());
    }

    // 泛型
    public static void test2() {
        Gson gson = new Gson();
        Result<String[]> r = Result.success(String[].class, new String[]{"hello", "gson"});
        System.out.println(r.toString());
        String s = gson.toJson(r);
        System.out.println(s);
        Result<String[]> r2 = gson.fromJson(s, new TypeToken<Result<String[]>>(){}.getType());
        Result<String[]> r3 = gson.fromJson(s, Result.class);
        System.out.println(r3);


    }


}
